import { Component, Vue, Watch } from 'vue-property-decorator';
import { cloneDeep, forEach, set, isArray, compact, unset } from "lodash";
import { isEmpty } from "@/utils"
/**
 * 这是一个查询头mixin类
 *
 * @Function onQueryClick 列表查询
 * @Function onQueryTreeClick 树形数据本地条件过滤
 * @Function onExportExcel 导出表格
 *
 * @export
 * @class mixinTableHeader
 * @extends {Vue}
 */
@Component({
})
export class mixinTableHeader extends Vue {
    // 数据变化动态监听列表
    watchList: any = {};
    headerSelectList = [];
    @Watch('headerSelectList', { immediate: true })
    updateStatusList(v: any) {
        this.updateEnquiryData(v);
    }
    updateEnquiryData(v: any) {
        if (!isEmpty(v)) {
            const me = this as any, searchHeader = me.$refs.searchHeader;
            if (searchHeader) {
                me.setEnquiryData(v, searchHeader);
            } else {
                me.$nextTick(() => {
                    me.setEnquiryData(v, me.$refs.searchHeader);
                })
            }
        }
    }

    setEnquiryData(v: any, searchHeader: any) {
        const me = this as any;
        const form = searchHeader.getForm();
        v.forEach((item: any) => {
            const {
                dataName, keyName, funNmae
            } = item, data = me[dataName];
            if (!isEmpty(data)) {
                form.setFieldData(keyName, data, item.isCascader)
            } else {
                me.watchList[dataName] = me.$watch(dataName, (v: any) => {
                    form.setFieldData(keyName, v, item.isCascader)
                })
                if (funNmae) {
                    me[funNmae]();
                }
            }
        })
    }

    beforeDestroy() {
        const me = this as any;
        // 销毁动态监听
        for (const item in me.watchList) {
            me.watchList[item]()
        }
    }

    /**
     * 列表查询
     * 与ux-header-search扩展结合使用，用于查询按钮调用
     * 此方法必须引入mixinTableStore类才能使用
     * @param {*} params
     * @memberof mixinTableHeader
     */
    onQueryClick(params: any) {
        const me = this as any;
        me.proxyQuery(me.getQueryValues(params));
    }

    /**
     * 获取查询头查询数据
     *
     * @return {*}
     * @memberof mixinTableQuery
     */
    getQueryValues(params: any): any {
        const me: any = this,
            // 拷贝数据，避免影响组件
            data = cloneDeep(params),
            // 获取提交数据处理配置
            transform = me.enquiryData.valueTransform || [];
        // 遍历配置
        forEach(transform, (item: any) => {
            // 字段名称
            const { isLast, field, fields, toArray, isToString } = item;
            let values: any = data[field];
            if (isToString && !isEmpty(values)) {
                // 是否转换为字符串
                set(data, field, values.toString())
            } else if (toArray && !isEmpty(values)) {
                // 是否转换为数组
                set(data, field, [values]);
            } else if (isArray(values)) {
                // 过滤掉空数据
                values = compact(data[field]);
                // 如果有值
                if (!isEmpty(values)) {
                    if (isLast) {
                        // 是否只提交最后一个字段
                        const length = values.length - 1;
                        set(data, fields[length], values[length]);
                    } else {
                        // 遍历输出字段组按顺序赋值
                        forEach(values, (item, i) => {
                            set(data, fields[i], item);
                        });
                    }
                    // 删除旧数据
                    unset(data, field);
                }
            } else if (values) {
                const list = me.query.options[field], {
                    idProperty, valueProperty
                } = item,
                    // 获取到数据对象
                    info = list.find((item: any) => {
                        return item[idProperty] == values;
                    })
                if (info) {
                    // 设置数据为复杂数据
                    set(data, field, info[valueProperty]);
                }
            }
        });
        return data;
    }

}
